from typing import Literal, Optional
from pydantic import BaseModel, Field

# %% 请求体
# 对于 images.generate 的请求体
class ImageGenerationRequest(BaseModel):
    class Config:
        frozen = True

    prompt: str = Field(..., description="生成图片的文本描述")
    # file:///Users/sf/Pyproj/openai-server-template/src/handler/images/__init__.py
    model: str = Field(
        "simulate", description="要使用的模型名称（例如 'simulate'）此处仅作兼容性标识"
    )
    n: int = Field(1, ge=1, le=10, description="生成图片的数量, 范围 1-10")
    quality: Literal["standard", "hd", "auto", "high"] = Field(
        "standard",
        description="图片质量，可以是 'standard' 或 'hd'",
    )
    response_format: Literal["url", "b64_json"] = Field(
        "url",
        description="响应格式，可以是 'url' 或 'b64_json'",
    )
    size: str = Field(
        "1024x1024",
        description="图片尺寸，例如 '1024x1024'",
        pattern="^(\d+x\d+)$",
    )
    style: Optional[Literal["natural", "vivid"]] = Field(
        None,
        description="图片风格缺省None,可以是 'vivid' 或 'natural'",
    )
    user: Optional[str] = Field(None, description="可选的用户标识符。")
    stream: bool = Field(False, description="是否启用流式响应，默认为 False")

# 对于 images.generate 流式响应中的图像, TODO 完善流式响应
class ImageGenPartialImage(BaseModel):
    class Config:
        frozen = True
        
    b64_json: str
    """Base64-encoded partial image data, suitable for rendering as an image."""
    background: Literal["transparent", "opaque", "auto"]
    """The background setting for the requested image."""
    created_at: int
    """The Unix timestamp when the event was created."""
    output_format: Literal["png", "webp", "jpeg"]
    """The output format for the requested image."""
    partial_image_index: int
    """0-based index for the partial image (streaming)."""
    quality: Literal["low", "medium", "high", "auto", "standard", "hd"]
    """The quality setting for the requested image."""
    size: Literal["1024x1024", "1024x1536", "1536x1024", "auto"]
    """The size of the requested image."""
    type: Literal["image_generation.partial_image"]
    """The type of the event. Always `image_generation.partial_image`."""

# %% 响应体

# 对于 images.generate 响应中的数据项
class GeneratedImageData(BaseModel):
    b64_json: str | None = None
    url: str | None = None
    revised_prompt: str | None = None  # DALL-E 3 会返回修改后的提示词


# 对于 images.generate 完整响应
class ImageGenerationResponse(BaseModel):
    created: int
    data: list[GeneratedImageData]

